// 示例 1：

// 输入: a = "11", b = "1"
// 输出："100"
// 示例 2：

// 输入：a = "1010", b = "1011"
// 输出："10101"


// 提示：

// 1 <= a.length, b.length <= 104
// a 和 b 仅由字符 '0' 或 '1' 组成
// 字符串如果不是 "0" ，就不含前导零

// let a = "11", b = "1"
// let a = "1010", b = "1011"
let a = '1', b = '0'

var addBinary = function (a, b) {
    let alen = a.length
    let blen = b.length
    while (alen > blen) {
        b = '0' + b
        blen++
    }
    while (blen > alen) {
        a = '0' + a
        alen++
    }
    let n = alen;
    let str = ''
    let jinwei = 0
    for (let i = n - 1; i >= 0; i--) {
        let sum = Number(a[i]) + Number(b[i]) + jinwei
        if (sum === 0) {
            str = '0' + str
            jinwei = 0
            console.log(1);

        } else if (sum === 1) {
            str = '1' + str
            jinwei = 0
            console.log(2);

        } else if (sum === 2) {
            str = '0' + str
            jinwei = 1
            console.log(3);

        } else if (sum === 3) {
            str = '1' + str
            jinwei = 1
            console.log(4);

        }
    }
    sum = Number(a[0]) + Number(b[0]) + jinwei
    if (sum === 2 || sum === 3) {
        str = '1' + str
    }

    return str

};
console.log(addBinary(a, b));
